package me.flyray.crm.core.feignserver;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.crm.request.FreezeRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.enums.FreezeStateEnums;
import com.github.wxiaoqi.security.common.exception.BusinessException;
import com.github.wxiaoqi.security.common.msg.BaseApiResponse;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import me.flyray.crm.core.biz.CommonFreezeUnFreezeBizTcc;
import me.flyray.crm.core.entity.FreezeJournal;
import me.flyray.crm.core.entity.UnfreezeJournal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/***
 * 冻结、解冻相关的接口
 * */
@Api(tags="冻结、解冻相关的TCC接口")
@Controller
@RequestMapping("feign")
public class FeignCustomerFreezeUnFreezeTcc {
	private static final Logger log = LoggerFactory.getLogger(FeignCustomerFreezeUnFreezeTcc.class);

	
	@Autowired
	private CommonFreezeUnFreezeBizTcc commonFreezeUnFreezeBizTcc;
	
	
	/***
	 * 个人或者商户冻结
	 * */
	@ApiOperation("冻结接口")
	@RequestMapping(value = "/freezeTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse freezeTcc(@RequestBody @Valid FreezeRequest freezeRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("FeignCustomerFreezeUnFreezeTcc冻结接口开始--请求参数{}"+EntityUtils.beanToMap(freezeRequest));
		response = commonFreezeUnFreezeBizTcc.freeze(freezeRequest);
		log.info("FeignCustomerFreezeUnFreezeTcc冻结接口结束--结果{}"+response);
		return response;
	}
	
	/***
	 * 个人或者商户冻结流水
	 * */
	@ApiOperation("冻结流水接口")
	@RequestMapping(value = "/freezeJournalTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse freezeJournalTcc(@RequestBody @Valid FreezeRequest freezeRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("freezeJournalTcc冻结流水接口开始--请求参数{}"+EntityUtils.beanToMap(freezeRequest));
		FreezeJournal freezeJournal=new FreezeJournal();		
		if(freezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
			freezeJournal.setCustomerNo(freezeRequest.getMerchantId());
		}else if(freezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
			freezeJournal.setCustomerNo(freezeRequest.getPersonalId());
		}			
		String freezeId =String.valueOf(SnowFlake.getId());
		freezeJournal.setJournalId(freezeId);
		freezeJournal.setAccountType(freezeRequest.getAccountType());
		freezeJournal.setPlatformId(freezeRequest.getPlatformId());
		freezeJournal.setCustomerType(freezeRequest.getMerchantType());
		freezeJournal.setTradeType(freezeRequest.getTradeType());//交易类型// 01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		freezeJournal.setOrderNo(freezeRequest.getOrderNo());
		freezeJournal.setFreezeBalance(new BigDecimal(freezeRequest.getFreezeAmt()));
		freezeJournal.setStatus(FreezeStateEnums.freeze.getCode());//冻结状态 1：已冻结  2：部分解冻  3：已解冻  4:待确认状态
		freezeJournal.setCreateTime(new Date());
		response=commonFreezeUnFreezeBizTcc.insertFreezeJournal(freezeJournal);
		log.info("freezeJournalTcc冻结流水接口结束--结果{}"+response);
		return response;
	}
	
	/***
	 * 个人或者商户解冻流水
	 * */
	@ApiOperation("校验冻结流水是否被解冻")
	@RequestMapping(value = "/verificationFreezeId",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse verificationFreezeId(@RequestBody @Valid UnFreezeRequest unFreezeRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("FeignCustomerFreezeUnFreezeTcc校验冻结流水是否被解冻--请求参数{}"+EntityUtils.beanToMap(unFreezeRequest));
		response=commonFreezeUnFreezeBizTcc.verificationFreezeId(unFreezeRequest);
		log.info("FeignCustomerFreezeUnFreezeTcc校验冻结流水是否被解冻--结果{}"+response);
		return response;
	}
	
	
	/**
	 * 个人或者商户解冻
	 * */
	@ApiOperation("解冻接口")
	@RequestMapping(value = "/unfreezeTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse unfreezeTcc(@RequestBody @Valid UnFreezeRequest unFreezeRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("FeignCustomerFreezeUnFreezeTcc解冻接口开始--请求参数{}"+EntityUtils.beanToMap(unFreezeRequest));
		response = commonFreezeUnFreezeBizTcc.unfreeze(unFreezeRequest);
		log.info("FeignCustomerFreezeUnFreezeTcc解冻接口结束--结果{}"+response);
		return response;
    }
	
	/***
	 * 个人或者商户解冻流水
	 * */
	@ApiOperation("解冻流水接口")
	@RequestMapping(value = "/unfreezeJournalTcc",method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse unfreezeJournalTcc(@RequestBody @Valid UnFreezeRequest unFreezeRequest){
		BaseApiResponse response=new BaseApiResponse();
		log.info("unfreezeJournalTcc解冻流水接口开始--请求参数{}"+EntityUtils.beanToMap(unFreezeRequest));
		UnfreezeJournal unfreezeJournal=new UnfreezeJournal();	
		String unfreezeId =String.valueOf(SnowFlake.getId());
		unfreezeJournal.setFreezeId(unFreezeRequest.getFreezeId());
		unfreezeJournal.setOrderNo(unFreezeRequest.getOrderNo());
		unfreezeJournal.setJournalId(unfreezeId);
		unfreezeJournal.setTradeType(unFreezeRequest.getTradeType());
		unfreezeJournal.setUnfreezeBalance(new BigDecimal(unFreezeRequest.getUnfreezeAmt()));
		unfreezeJournal.setCreateTime(new Date());
		response=commonFreezeUnFreezeBizTcc.insertUnfreezeJournal(unfreezeJournal);
		log.info("unfreezeJournalTcc解冻流水接口结束--结果{}"+response);
		return response;
	}

}
